package cn.gson.crm.controller.dept;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.entity.Dept;

@WebServlet("/dept/sondept/delt")
public class SonDeptDeletServlet extends BaseServlet{
	/**
	 * 子部门删除
	 */
	private static final long serialVersionUID = 1L;
	private DeptDao deptDao = new DeptDao();

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] deletIds = request.getParameterValues("id");
		//不能删除的部门集合
		List<Dept> cantBedeleDeptList = new ArrayList<>();
		//只删除被选中的id子部门，不包括自己
		for (int i = deletIds.length-1; i > 0; i--) {
			//判断是否有子部门
			List<Dept> deptChild = deptDao.getSonDept(1, deletIds[i]);
			if(deptChild==null){
				deptDao.deletDepartment(0, deletIds[i]);
			}else{
				Dept cantBedeleDept = deptDao.getDeptById(1,  deletIds[i]);
				cantBedeleDeptList.add(cantBedeleDept);
			}
			if(cantBedeleDeptList.size()>0){
				request.getSession().setAttribute("cantBeDeleDeptList", cantBedeleDeptList);
				request.getSession().setAttribute( "deleErro","有子部门，不能被删除");
			}
		}
		redirect("dept/son?id="+deletIds[0]);
	}
}
